package com.cdvtc.flower.dao.impl;

import com.cdvtc.flower.dao.CustomerDao;
import com.cdvtc.flower.dao.ProductsDao;
import com.cdvtc.flower.model.Customer;
import com.cdvtc.flower.model.Merchants;
import com.cdvtc.flower.util.DBUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class CustomerDaoImpl implements CustomerDao {
    private Connection conn;
    @Override
    public List<Customer> getAllCustomer() {
        List<Customer> customers = new ArrayList<>();

        // 获取连接
        conn = DBUtil.getConnection();
//        Statement st = null;


        // 创建Staement
        try(Statement st = conn.createStatement()) {
            // 执行SQL，获取ResultSet
            String sql = "select * from customer";
            try(ResultSet rs = st.executeQuery(sql)) {
                // 从rs中获取数据
                while (rs.next()) {
                    int customer_id = rs.getInt("customer_id");
                    String user_name = rs.getString("user_name");
                    String address = rs.getString("address");
                    String phone = rs.getString("phone");
                    String email = rs.getString("email");
                    String password = rs.getString("password");

                    // 将数据写入封装对象
                    Customer customer = new Customer();

                    customer.setCustomer_id(customer_id);
                    customer.setEmail(email);
                    customer.setPassword(password);
                    customer.setPhone(phone);
                    customer.setUser_name(user_name);
                    customer.setAddress(address);

                    //将对象往往集合类中
                    customers.add(customer);
                }
            }

            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return customers;
    }
    public static void main(String[] args) {
        CustomerDao customerDao = new CustomerDaoImpl();
        System.out.println(customerDao.getAllCustomer());
    }
}
